///////////////////////////////////////////////////////////////////////////////
// File:        RealmList.cc
// Author:      671643387@qq.com
// Description: 
///////////////////////////////////////////////////////////////////////////////

#include "Log.hpp"
#include "Realmd.hpp"
#include "RealmList.hpp"

#define REALM_UPDATE_INTERVAL   60000
///////////////////////////////////////////////////////////////////////////////
RealmList::RealmList(void)
    : updateTime_(0)
{

}

RealmList::~RealmList(void)
{

}

///////////////////////////////////////////////////////////////////////////////
void RealmList::Update(void)
{
    TIME_T now = FromStartup();
    if ((now - updateTime_) < REALM_UPDATE_INTERVAL)
        return;

    updateTime_ = now;
    realms_.clear();

    DEBUG_LOG("load realms from database");

    SqlResultSetPtr result = Realmd::GetDB()->PQuery("SELECT * FROM realmlist;");
    if (result)
    {
        do 
        {
            struct Realm realm = {0};
            realm.id = (*result)[0]->GetUInt32();
            realm.name = (*result)[1]->GetString();
            realm.address.append((*result)[2]->GetCString())
                .append(":").append((*result)[3]->GetCString());
            realm.icon = (*result)[4]->GetUInt8();
            realm.realmflags = (eRealmFlags)(*result)[5]->GetUInt8();
            realm.timezone = (*result)[6]->GetUInt8();
            realm.populationLevel = (*result)[8]->GetFloat();
            realms_.push_back(realm);
        } while (result->NextRow());
    }
}